package com.example.howard.picview.PicView;

import android.app.Activity;
import android.content.Intent;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageSwitcher;
import android.widget.ImageView;

import com.example.howard.picview.MyApplication;
import com.example.howard.picview.R;
import com.squareup.picasso.Picasso;

import java.io.File;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;

import uk.co.senab.photoview.PhotoView;
import uk.co.senab.photoview.PhotoViewAttacher;

public class PicViewActivity extends Activity {
    private ImageSwitcher is;
    private ImageView i;
    private int downX, upX;// index = 0;
    private List<String> imageRes = new ArrayList<>();
    private MyApplication myApplication;
    private int pos;
    private ViewPager viewPager;




    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.pic_view);
        Intent intent=getIntent();
        Bundle bundle=intent.getExtras();
        pos=bundle.getInt("pos");
        myApplication= (MyApplication) getApplication();
        imageRes=myApplication.showimage;
//        PhotoView photoView = new PhotoView(this);

        viewPager = (ViewPager) findViewById(R.id.viewPager);
//给viewPager设置adapter，将每个图片设置到每个页面当中
        viewPager.setAdapter(new MyPagerAdapter());
        viewPager.setCurrentItem(pos);

        //给viewPager设置监听
        viewPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {

            @Override
            public void onPageSelected(int arg0) {

                int childCount = viewPager.getChildCount();//viewPager得到页面的数量

                // 遍历当前所有加载过的PhotoView，恢复所有图片的默认状态

                for (int i = 0; i < childCount; i++) {
                    View childAt = viewPager.getChildAt(i);

                    try {
                        if (childAt != null && childAt instanceof PhotoView) {
                            PhotoView  photoView = (PhotoView) childAt;//得到viewPager里面的页面
                            PhotoViewAttacher mAttacher = new PhotoViewAttacher(photoView);//把得到的photoView放到这个负责变形的类当中
                            //mAttacher.getDisplayMatrix().reset();//得到这个页面的显示状态，然后重置为默认状态
                            mAttacher.setScaleType(ImageView.ScaleType.FIT_CENTER);//设置屏幕适配
                        }
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }
            }

            @Override
            public void onPageScrolled(int arg0, float arg1, int arg2) {
                // TODO Auto-generated method stub

            }

            @Override
            public void onPageScrollStateChanged(int arg0) {
                // TODO Auto-generated method stub

            }
        });

    }
    /**
     * 自定义pagerAdapter
     */
    public class MyPagerAdapter extends PagerAdapter {

        @Override
        public int getCount() {
            return imageRes.size();
        }
        @Override
        public View instantiateItem(ViewGroup container, int position) {
            PhotoView photoView = new PhotoView(container.getContext());
//            photoView.setOnPhotoTapListener(new PhotoViewAttacher.OnPhotoTapListener() {
//                @Override
//                public void onPhotoTap(View view, float x, float y) {
//                    PicViewActivity.this.finish();
////                finish();
//                }
//            });
            container.addView(photoView, ViewPager.LayoutParams.MATCH_PARENT, ViewPager.LayoutParams.MATCH_PARENT);
//            photoView.setImageResource(Integer.parseInt(imageRes.get(pos)));

            Picasso
                                .with(PicViewActivity.this)
                                .load(new File(imageRes.get(position)))
                                .resize(1000,1000)
                                .centerInside()
                                .placeholder(R.drawable.picassopic)
                                .error(R.drawable.picassopic)
                                .into(photoView);
            photoView.setScaleType(ImageView.ScaleType.FIT_CENTER);//设置图片屏幕适配
            return photoView;
        }

        @Override
        public void destroyItem(ViewGroup container, int position, Object object) {
            container.removeView((View) object);
        }

        @Override
        public boolean isViewFromObject(View view, Object object) {
            return view == object;
        }
    }

}
//    private void initView() {
//        //设置ImageView控件
//        i= (ImageView) findViewById(R.id.imageView);
//
//        // 设置ImageSwitcher控件
//        is = (ImageSwitcher) findViewById(R.id.imageswitcher);
//        is.setFactory(new ViewSwitcher.ViewFactory() {
//            @Override
//            public View makeView() {
//                ImageView imageView = new ImageView(PicViewActivity.this);
////				imageView.setBackgroundColor(0xFF000000);
//                imageView.setScaleType(ImageView.ScaleType.CENTER);// 居中
////                imageView.setLayoutParams(new ImageSwitcher.LayoutParams(Gallery.LayoutParams.FILL_PARENT, Gallery.LayoutParams.FILL_PARENT));
//                return imageView;
//            }
//        });
//        is.setInAnimation(AnimationUtils.loadAnimation(this,
//                android.R.anim.fade_in));
//        is.setOutAnimation(AnimationUtils.loadAnimation(this,
//                android.R.anim.fade_out));
//        is.setOnTouchListener(new View.OnTouchListener() {
//            @Override
//            public boolean onTouch(View v, MotionEvent event) {
//                if (event.getAction() == MotionEvent.ACTION_DOWN) {
//                    downX = (int) event.getX();// 取得按下时的坐标
//                    return true;
//                } else if (event.getAction() == MotionEvent.ACTION_UP) {
//                    upX = (int) event.getX();// 取得松开时的坐标
////                    int index = pos;
//                    if (upX - downX > 100)// 从左拖到右，即看前一张
//                    {
//                        // 如果是第一，则去到尾部
//                        if (pos == 0){
//                            pos = imageRes.size() - 1;
//                        }else{
//                            pos = pos - 1;
//                        }
//                        Picasso
//                                .with(PicViewActivity.this)
//                                .load(new File(imageRes.get(pos)))
//                                .resize(1000,1000)
//                                .centerInside()
//                                .placeholder(R.drawable.picassopic)
//                                .error(R.drawable.picassopic)
//                                .into(i);
//                    } else if (downX - upX > 100)// 从右拖到左，即看后一张
//                    {
//                        // 如果是最后，则去到第一
//                        if (pos == (imageRes.size() - 1)){
//                            pos = 0;
//                        }else{
//                            pos = pos + 1;
//                        }
//
//                        Picasso
//                                .with(PicViewActivity.this)
//                                .load(new File(imageRes.get(pos)))
//                                .resize(1000,1000)
//                                .centerInside()
//                                .placeholder(R.drawable.picassopic)
//                                .error(R.drawable.picassopic)
//                                .into(i);
////                        Drawable drawable = Drawable.createFromPath(imageRes.get(pos));
////                        i.setImageDrawable(drawable);
//                    }else {
//                       PicViewActivity.this.finish();
//                    }
//                    // 改变gallery图片所选，自动触发ImageSwitcher的setOnItemSelectedListener
////                    gallery.setSelection(index, true);
//
//                    return true;
//                }
//                return false;
//            }
//
//        });
//        Picasso
//                .with(PicViewActivity.this)
//                .load(new File(imageRes.get(pos)))
//                .resize(1000,1000)
//                .centerInside()
//                .placeholder(R.drawable.picassopic)
//                .error(R.drawable.picassopic)
//                .into(i);
//    }
//        gallery = (Gallery) findViewById(R.id.gallery);
//        gallery.setAdapter(new ImageAdapter(this));
//        gallery.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
//            @Override
//            public void onItemSelected(AdapterView<?> parent, View view,
//                                       int position, long id) {
//                Drawable drawable=Drawable.createFromPath(imageRes.get(position));
//                is.setImageDrawable(drawable);
//            }
//
//            @Override
//            public void onNothingSelected(AdapterView<?> parent) {
//
//
//            }
//
//        });
//
//    }
//
//    @Override
//    public boolean onCreateOptionsMenu(Menu menu) {
//        // Inflate the menu; this adds items to the action bar if it is present.
////        getMenuInflater().inflate(R.menu.main, menu);
//        return true;
//    }
//
//    public class ImageAdapter extends BaseAdapter {
//        public ImageAdapter(Context c) {
//            mContext = c;
//        }
//
//        public int getCount() {
//            return imageRes.size();
//        }
//
//        public Object getItem(int position) {
//            return position;
//        }
//
//        public long getItemId(int position) {
//            return position;
//        }
//
//        public View getView(int position, View convertView, ViewGroup parent) {
//            ImageView i = new ImageView(mContext);
//            Drawable drawable=Drawable.createFromPath(imageRes.get(position));
//            i.setImageDrawable(drawable);
//            i.setAdjustViewBounds(true);
//            i.setLayoutParams(new Gallery.LayoutParams(
//                    Gallery.LayoutParams.WRAP_CONTENT, Gallery.LayoutParams.WRAP_CONTENT));
//            return i;
//        }
//
//        private Context mContext;
//    }
//}
